package com.atguigu.connection;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author admin
 */
public class C3P0Test {

    /**
     * 方式一：
     * @throws Exception
     */
    @Test
    public void testGetConnection() throws Exception {
        // 获取 c3p0 数据库连接池
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/atguigu_jdbc");
        comboPooledDataSource.setUser("root");
        comboPooledDataSource.setPassword("123456");

        // 设置初始时数据库连接池中的连接数
        comboPooledDataSource.setInitialPoolSize(10);

        Connection connection = comboPooledDataSource.getConnection();
        System.out.println(connection);

        // 销毁 c3p0 数据库连接池
        // DataSources.destroy(comboPooledDataSource);
    }

    /**
     * 方式二：使用配置文件
     */
    @Test
    public void testGetConnection1() throws SQLException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("helloc3p0");
        Connection connection = comboPooledDataSource.getConnection();
        System.out.println(connection);
    }
}
